<?php

namespace app\admin\controller\separate_order;

use app\admin\controller\SeparateSet;
use app\admin\model\Group;
use app\admin\model\Store;
use app\api\model\SeparateAccounts;
use app\api\model\ShareOrder;
use app\common\controller\Backend;
use think\Request;
use think\Session;
use wechat\WechatController;
use wechat\WechatService;

class Order extends Backend
{
    protected $searchFields = 'separate_accounts.id,order_id';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new SeparateAccounts();
        $this->service_secret = Session::get('admin')['secret'];
        if(!empty($this->service_secret)){
            $this->model->alias("separate_accounts")
                ->where(['separate_accounts.service_secret'=>$this->service_secret]);
        }
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with('store')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function info($ids = null){
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $order_id = $this->model->where(['id'=>$params['ids']])->value("order_id");
            $this->model = new ShareOrder();
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['store'])
                ->where($where)
                ->where(['order_id'=>$order_id])
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list->items() as &$item) {
                $item->amount /= 100;
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $order = $this->model->where(['id'=>$ids])->find();
        $store = Store::get(['sub_mch_id' => $order->sub_mch_id]);
        $amount = round($order->amount * ($store->platform_commission / 100),2);
        $this->assign('amount',$amount);
        $this->model = new ShareOrder();
        $amount = $this->model->where(['order_id'=>$order->order_id])->sum("amount");
        $this->assign("platform_amount",$amount / 100);
        return $this->view->fetch();
    }
    public function share_del($ids = null){
        $this->model = new ShareOrder();
        return parent::del();
    }
    public function retransmission($ids = null){
        $separate = new \app\api\model\SeparateSet();
        $arr = $separate->where("secret",$this->service_secret)->find();
        $order = $this->model->where(['id'=>$ids])->find();
        $this->wechat = new WechatController($order->sub_mch_id,$arr);
        $share_order = ShareOrder::all(['order_id'=>$order->order_id]);
        $res = $this->wechat->separate_accounts($share_order,$order->transaction_id);
        if ($res['result_code'] == "FAIL")
            $this->error($res['result_message']);
        $this->success("分账成功");
    }
}